//
//  BaseTableCell.m
//  Budejie
//
//  Created by 远成物流 on 2017/5/16.
//  Copyright © 2017年 陈梦佳. All rights reserved.
//

#import "BaseTableCell.h"
#import "UIImageView+Download.h"

@interface BaseTableCell ()

@property (weak, nonatomic) IBOutlet UIImageView *profileImage;//头像
@property (weak, nonatomic) IBOutlet UILabel *nickName;//昵称
@property (weak, nonatomic) IBOutlet UILabel *createTime;//发布时间
@property (weak, nonatomic) IBOutlet UILabel *content;//正文
@property (weak, nonatomic) IBOutlet UIButton *moreButton;//更多
@property (weak, nonatomic) IBOutlet UIButton *dingButton;//顶
@property (weak, nonatomic) IBOutlet UIButton *caiButton;//踩
@property (weak, nonatomic) IBOutlet UIButton *shareButton;//分享
@property (weak, nonatomic) IBOutlet UIButton *commentButton;//评论
@property (weak, nonatomic) IBOutlet UIView *commentView;//最热评论
@property (weak, nonatomic) IBOutlet UILabel *commentText;//评论内容

@end

@implementation BaseTableCell

#pragma 懒加载
-(CellVideoView *)videoView{
    if (!_videoView) {
        CellVideoView *video = [[NSBundle mainBundle] loadNibNamed:@"CellVideoView" owner:self options:nil].lastObject;
        [self.contentView addSubview:video];
        
        _videoView = video;
    }
    
    return _videoView;
}

-(CellPictureView *)pictureView{
    if (!_pictureView) {
        CellPictureView *picture = [[NSBundle mainBundle] loadNibNamed:@"CellPictureView" owner:self options:nil].lastObject;
        [self.contentView addSubview:picture];
        _pictureView = picture;
    }
    
    return _pictureView;
}

-(CellVoiceView *)voiceView{
    if (!_voiceView) {
        CellVoiceView *voice = [[NSBundle mainBundle] loadNibNamed:@"CellVoiceView" owner:self options:nil].lastObject;
        [self.contentView addSubview:voice];
        _voiceView = voice;
    }
    
    return _voiceView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainCellBackground"]]];
}

-(void)setTopic:(EssenceTopic *)topic{
    
    _topic = topic;
    
    //顶部
    [self.profileImage setImageWithUrl:topic.profile_image];
    self.nickName.text = topic.name;
    self.createTime.text = topic.passtime;
    self.content.text = topic.text;
    
    //中间
    if (topic.type == EssenceTopicTypeWord) {
        //段子
        self.pictureView.hidden = YES;
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
    }else if (topic.type == EssenceTopicTypeVideo){
        //视频
        self.videoView.hidden = NO;
        self.videoView.topic = topic;
        self.pictureView.hidden = YES;
        self.voiceView.hidden = YES;
    }else if (topic.type == EssenceTopicTypeVoice){
        //声音
        self.voiceView.hidden = NO;
        self.voiceView.topic = topic;
        self.pictureView.hidden = YES;
        self.videoView.hidden = YES;
    }else{
        //图片
        self.pictureView.hidden = NO;
        self.pictureView.topic = topic;
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
    }
    
    //底部
    [self setButton:self.dingButton number:topic.ding placeholder:@"顶"];
    [self setButton:self.caiButton number:topic.cai placeholder:@"踩"];
    [self setButton:self.shareButton number:topic.repost placeholder:@"分享"];
    [self setButton:self.commentButton number:topic.comment placeholder:@"评论"];
    
    //最热评论
    if (topic.top_cmt.count) {
        //有最热评论
        self.commentView.hidden = NO;
        
        NSDictionary *dict = topic.top_cmt.firstObject;
        NSString *content = dict[@"content"];
        if (content.length == 0) {
            content = @"语音评论";
        }
        
        NSString *commentUserName = dict[@"user"][@"username"];
        self.commentText.text = [NSString stringWithFormat:@"%@:%@",commentUserName,content];
    }else{
        //没有最热
        self.commentView.hidden = YES;
    }
}

-(void)setButton:(UIButton *)button number:(NSInteger)number placeholder:(NSString *)placeholder{
    
    if (number >= 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万",number/10000.0] forState:UIControlStateNormal];
    }else if (number > 0){
        [button setTitle:[NSString stringWithFormat:@"%zd",number] forState:UIControlStateNormal];
    }else{
        [button setTitle:placeholder forState:UIControlStateNormal];
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    __block BaseTableCell *cell = self;
    
    if (self.topic.type == EssenceTopicTypeVideo) {
        self.videoView.frame = self.topic.middleFrame;
        cell.videoView.playVideo = ^(UIButton *btn){
            self.playBlock(btn);
        };
    }else if (self.topic.type == EssenceTopicTypeVoice){
        self.voiceView.frame = self.topic.middleFrame;
    }else if (self.topic.type == EssenceTopicTypePicture){
        self.pictureView.frame = self.topic.middleFrame;
    }
}

- (void)setFrame:(CGRect)frame
{
    frame.size.height -= B_Margin;
    
    [super setFrame:frame];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
